package cn.itsource.org.service.impl;

import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.util.BaiduAuditUtils;
import cn.itsource.basic.util.Md5Utils;
import cn.itsource.basic.util.StrUtils;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.domain.ShopAuditLog;
import cn.itsource.org.dot.ShopDto;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopAuditLogMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.org.service.IShopService;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.user.domain.Logininfo;
import cn.itsource.user.mapper.LogininfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;

/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public void settlement(Shop shop) {
        //1检验空值  前端js检验容易禁用，不安全后端也要检验
        if(StringUtils.isEmpty(shop.getName())){
            throw new BusinessException("提交资料不能为空");
        }

        //校验店铺名称
        if(!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("店铺名称不合法!!!");
        }
        //校验logo
        if(!StringUtils.isEmpty(shop.getLogo())){
            if(!BaiduAuditUtils.ImgCensor("http://123.207.27.208"+shop.getLogo())){
                throw new BusinessException("logo不合法!!!");
            }
        }

        //2检验店铺是否入住过
        Shop dbshop = shopMapper.findByNameAnaAddress(shop);
        if(null != dbshop){//入住过
            throw new BusinessException("请勿重复入住，忘记密码请联系管理员");
        }
        //二：添加管理员信息 - 添加了之后就可以获取自增ID
        Employee admin = shop.getAdmin();

        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(admin.getPassword() + salt);
        admin.setSalt(salt);
        admin.setPassword(md5Pwd);

        //保存logininfo
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(admin,logininfo);
        logininfo.setType(0);//管理员
        logininfo.setDisable(true);
        logininfoMapper.add(logininfo);

        admin.setLogininfoId(logininfo.getId());
        employeeMapper.add(admin);

        //三：将自增id设置到t_shop的admin_id
        shop.setAdminId(admin.getId());

        //四：添加Shop
        shopMapper.add(shop);

        //五：将shop_id更新到t_employee
        admin.setShopId(shop.getId());
        employeeMapper.update(admin);
    }

    @Override
    public void pass(ShopAuditLog log) throws MessagingException { //shopId note
        //改状态 保存审核日志 发邮件
        Shop shop = shopMapper.findById(log.getShopId());
        shop.setState(2);
        shopMapper.update(shop);

        //保存审核日志
        log.setState(2);
        //log.setAuditId("审核人=登陆人");
        shopAuditLogMapper.add(log);

        //发邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        helper.setFrom("1678656018@qq.com");
        helper.setSubject("店铺激活邮件");
        //true - 能够再内容中编写html标签 - 会解析
        helper.setText("<h3>你的店铺已经审核通过，请<a href='http://localhost:8080/shop/active/"+log.getShopId()
                +"'>点击这里</a>激活邮件</h3>", true);
        //店铺管理员
        Employee admin = employeeMapper.findById(shop.getAdminId());
        helper.setTo(admin.getEmail());
        //发送
        javaMailSender.send(mimeMessage);
    }

    @Override
    public void reject(ShopAuditLog log) throws MessagingException {
        //改状态 保存审核日志 发邮件
        Shop shop = shopMapper.findById(log.getShopId());
        shop.setState(4);
        shopMapper.update(shop);

        //保存审核日志
        log.setState(4);
        //log.setAuditId("审核人=登陆人");
        shopAuditLogMapper.add(log);

        //发邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        helper.setFrom("1678656018@qq.com");
        helper.setSubject("店铺激活失败通知");
        //true - 能够再内容中编写html标签 - 会解析
        helper.setText("<h3>你的店铺审核未通过，请<a href='http://localhost:8081/#/regiseterEdit/"+log.getShopId()
                +"'>点击这里</a>修改入驻信息重新提交</h3>", true);
        //店铺管理员
        Employee admin = employeeMapper.findById(shop.getAdminId());
        helper.setTo(admin.getEmail());
        //发送
        javaMailSender.send(mimeMessage);
    }

    @Override
    public List<ShopDto> echarts() {
        return shopMapper.echarts();
    }

    @Override
    public void active(Long id) {
        Shop shop = shopMapper.findById(id);
        shop.setState(3);
        shopMapper.update(shop);
    }


}
